package com.springboot2.controller;

import com.springboot2.entity.Menu;
import com.springboot2.service.MenuService;
import com.springboot2.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu/")
public class MenuController {
    @Resource
    private MenuService menuService;
    //增
    @PostMapping("add")
    public Result save(@RequestBody Menu menu){
        menuService.add(menu);
        return Result.success();
    }
    //删
    @DeleteMapping("del/{id}")
    public Result del(@PathVariable int id){
        menuService.del(id);
        return Result.success();
    }
    //改
    @PutMapping("update")
    public Result update(@RequestBody Menu menu){
        menuService.update(menu);
        return Result.success();
    }
    //获取对象
    @GetMapping("find/{id}")
    public Result findById(@PathVariable int id){
        return Result.success(menuService.findById(id));
    }
    //列表
    @GetMapping("list")
    public Result list(){
        return Result.success(menuService.findByList());
    }

    //把有的菜单项
    @GetMapping("lists")
    public Result lists(){
        return Result.success(menuService.lists());
    }
    //分页列表
    @GetMapping("findByPage/{pageNo}/{pageSize}")
    public Result findByPage(@PathVariable int pageNo,@PathVariable int pageSize){
        Map<String,Object> map = new HashMap<>();
        map.put("data",menuService.findByPage(pageNo, pageSize));
        map.put("total",menuService.count());
        return Result.success(map);
    }
    //查询条件
    @GetMapping("findByPageParams/{pageNo}/{pageSize}/{name}")
    public Result findByPageParams(@PathVariable int pageNo,@PathVariable int pageSize,@PathVariable String name){
        Map<String,Object> map = new HashMap<>();
        map.put("data",menuService.findByPageParams(pageNo, pageSize, name));
        map.put("total",menuService.countParams());
        return Result.success(map);
    }
    //查询条件-- 菜单名称
    @GetMapping("find/name")
    public Result findByName(@RequestParam(required = false) String name){
       Map<String,Object> map = new HashMap<>();
       map.put("data",menuService.findByName(name));
        return Result.success(map);
    }
    //批量删除
    @DeleteMapping("batch/{ids}")
    public Result dels(@PathVariable String ids){
        String [] split = ids.split(",");
        for (String s:split){
            menuService.del(Integer.parseInt(s));
        }
        return Result.success();
    }

}
